﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing.Imaging;
using System.Drawing;
using HalconDotNet;
using System.IO;
using System.Runtime.InteropServices;

namespace FinalTest_Algonrithm
{
    public class HalHelper
    {
        public static void Bitmap2HObject(Bitmap bmp, ref HObject image)
        {
            try
            {
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat);
                if (bmp.PixelFormat == PixelFormat.Format24bppRgb)
                {
                    unsafe
                    {
                        HOperatorSet.GenImageInterleaved(out image, srcBmpData.Scan0, "bgr", bmp.Width, bmp.Height, 0, "byte", bmp.Width, bmp.Height, 0, 0, -1, 0);
                    }
                }
                else
                {
                    HOperatorSet.GenImage1(out image, "byte", bmp.Width, bmp.Height, srcBmpData.Scan0);
                }
                bmp.UnlockBits(srcBmpData);
            }
            catch (Exception ex)
            {
                image.Dispose();
            }
        }

        public static unsafe bool ImageToPionter(ref HObject ho_image, ref HTuple byPtr)
        {
            try
            {
                HTuple h_type, h_w, h_h;
                HOperatorSet.GetImagePointer1(ho_image, out byPtr, out h_type, out h_w, out h_h);
            }
            catch (Exception ex)
            {
                byPtr = null;
                return false;
            }
            return true;
        }

        public static unsafe bool ImageGamaToPionter(ref HObject ho_image, bool black, double thre, ref HTuple byPtr)
        {
            try
            {
                HTuple h_type, h_w, h_h;
                HObject hObject;
                HOperatorSet.GenEmptyObj(out hObject);
                if (!black)
                {
                    HOperatorSet.Threshold(ho_image, out hObject, 0, thre - 20);
                    HOperatorSet.PaintRegion(hObject, ho_image, out ho_image, 0, "fill");
                    //HOperatorSet.WriteImage(ho_image, "bmp", 0, "D:\\10");
                }
                else
                {
                    HOperatorSet.Threshold(ho_image, out hObject, thre + 20, 255);
                    HOperatorSet.PaintRegion(hObject, ho_image, out ho_image, 255, "fill");
                }

                HOperatorSet.GetImagePointer1(ho_image, out byPtr, out h_type, out h_w, out h_h);
                hObject.Dispose();
            }
            catch (Exception ex)
            {
                byPtr = null;
                return false;
            }
            return true;
        }

        public static void ImageCrop(Bitmap bitmap, int sX, int sY, int w, int h, ref Bitmap bt)
        {
            bt = bitmap.Clone(new Rectangle(sX, sY, w, h), bitmap.PixelFormat);
        }

        public static bool GetLight(HObject ho_GrayImage, HObject[] hObjects, ref double[] lights)
        {
            try
            {
                HTuple Min, Max, Rang;

                for (int i = 0; i < hObjects.Length; i++)
                {
                    HOperatorSet.MinMaxGray(hObjects[i], ho_GrayImage, 1, out Min, out Max, out Rang);
                    lights[i] = (Max + Min) / 2;
                }
            }
            catch (Exception)
            {
            }
            return true;
        }
        public static bool GetAngel(HObject[] hObjects, ref double angel)
        {
            HTuple Arae1, row1, col1;
            HTuple Arae2, row2, col2;
            HTuple Arae3, row3, col3;
            HTuple Arae4, row4, col4;
            HOperatorSet.AreaCenter(hObjects[1], out Arae1, out row1, out col1);
            HOperatorSet.AreaCenter(hObjects[2], out Arae2, out row2, out col2);
            HOperatorSet.AreaCenter(hObjects[3], out Arae3, out row3, out col3);
            HOperatorSet.AreaCenter(hObjects[4], out Arae4, out row4, out col4);
            //double a1 = MyMath.Angle(col1, col2, row1, row2);
            //double a2 = MyMath.Angle(col3, col4, row3, row4);
            //angel = (a1 + a2) / 2;
            return true;
        }

    }
}
